package io.renren.modules.communityService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import io.renren.modules.communityService.enums.DbOneClickAlarmStatus;
import io.renren.modules.miniapp.dto.MiniAppDbOneClickAlarmListDTO;
import io.renren.modules.miniapp.dto.MiniAppDbOneClickAlarmSaveDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.communityService.dao.DbOneClickAlarmDao;
import io.renren.modules.communityService.entity.DbOneClickAlarmEntity;
import io.renren.modules.communityService.dto.DbOneClickAlarmDTO;
import io.renren.modules.communityService.vo.DbOneClickAlarmVO;
import io.renren.modules.communityService.service.DbOneClickAlarmService;
import io.renren.modules.communityService.conver.DbOneClickAlarmConver;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;


@Service("dbOneClickAlarmService")
public class DbOneClickAlarmServiceImpl extends ServiceImpl<DbOneClickAlarmDao, DbOneClickAlarmEntity> implements DbOneClickAlarmService {

    @Override
    public PageUtils<DbOneClickAlarmVO> queryPage(DbOneClickAlarmDTO dbOneClickAlarm) {
        IPage<DbOneClickAlarmEntity> page = baseMapper.selectPage(
                new Query<DbOneClickAlarmEntity>(dbOneClickAlarm).getPage(),
                new QueryWrapper<DbOneClickAlarmEntity>()
        );

        return PageUtils.<DbOneClickAlarmVO>page(page).setList(DbOneClickAlarmConver.MAPPER.conver(page.getRecords()));
    }
    @Override
    public DbOneClickAlarmVO getById(Integer id) {
        return DbOneClickAlarmConver.MAPPER.conver(baseMapper.selectById(id));
    }

    @Override
    public boolean save(DbOneClickAlarmDTO dbOneClickAlarm) {
        DbOneClickAlarmEntity dbOneClickAlarmEntity = DbOneClickAlarmConver.MAPPER.converDTO(dbOneClickAlarm);
        return this.save(dbOneClickAlarmEntity);
    }

    @Override
    public boolean updateById(DbOneClickAlarmDTO dbOneClickAlarm) {
        DbOneClickAlarmEntity dbOneClickAlarmEntity = DbOneClickAlarmConver.MAPPER.converDTO(dbOneClickAlarm);
        return this.updateById(dbOneClickAlarmEntity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MiniAppDbOneClickAlarmSaveDTO dto) {
        DbOneClickAlarmEntity dbOneClickAlarmEntity = DbOneClickAlarmConver.MAPPER.conver1(dto);
        dbOneClickAlarmEntity.setCreatedAt(new Date());
        dbOneClickAlarmEntity.setUpdatedAt(new Date());
        dbOneClickAlarmEntity.setStatus(DbOneClickAlarmStatus.ZERO.getKey());
        this.save(dbOneClickAlarmEntity);
    }

    @Override
    public PageUtils queryPage(MiniAppDbOneClickAlarmListDTO dbOneClickAlarm) {
        IPage<DbOneClickAlarmEntity> page = baseMapper.selectPage(
                new Query<DbOneClickAlarmEntity>(dbOneClickAlarm).getPage(),
                new QueryWrapper<DbOneClickAlarmEntity>().lambda()
                .eq(ObjectUtil.isNotNull(dbOneClickAlarm.getUserId()),DbOneClickAlarmEntity::getUserId,dbOneClickAlarm.getUserId())
        );

        return PageUtils.<DbOneClickAlarmVO>page(page).setList(DbOneClickAlarmConver.MAPPER.conver(page.getRecords()));
    }

}
